﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using WcfiUserService.Models;

namespace WcfiUserService
{
    /// <summary>
    /// Summary description for iUserService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class iUserService : System.Web.Services.WebService
    {
        private bool IsUserValid(string username, string password)
        {
            return Membership.ValidateUser(username, password);
        }


        [WebMethod]
        public List<UserView> GetUsers()
        {
            List<UserView> _users = new List<UserView>();
            _users.Add(new UserView() { UserName = "taipm" });
            _users.Add(new UserView() { UserName = "vietnh" });
            _users.Add(new UserView() { UserName = "manpth" });
            return _users;
        }

        [WebMethod]
        public bool Register(string username, string password, string email)
        {
            MembershipCreateStatus _status;
            MembershipUser _user;
            try
            {
                _user = Membership.CreateUser(username, password, email, "Your email ?", email, true, out _status);
                return true;
            }
            catch
            {
                _user = null;
            }

            return false;
        }

        [WebMethod]
        public bool Login(string userName, string password)
        {
            bool _result = false;
            _result = Membership.ValidateUser(userName, password);
            return _result;
        }


        [WebMethod]
        public bool ChangePassword(string userName, string curentpassword, string newpassword)
        {
            bool changePasswordSucceeded;
            try
            {
                MembershipUser currentUser = Membership.GetUser(userName, true);
                changePasswordSucceeded = currentUser.ChangePassword(curentpassword, newpassword);
            }
            catch (Exception)
            {
                changePasswordSucceeded = false;
            }
            if (changePasswordSucceeded)
                return true;
            else
                return false;

        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}
